import 'package:flutter/material.dart';
import 'package:ileddemo/code/ui/base/BaseWidget.dart';
import 'package:ileddemo/code/ui/base/TitleBarWidget.dart';
import 'package:lib_util/FileUtil.dart';
import 'package:image/image.dart' as img;
import 'package:lib_util/ToastUtil.dart';
import '../../../generated/l10n.dart';
import '../../ble/protocol/iledcolor/source/SourceManager.dart';
import '../../ble/protocol/iledcolor/source/SourceSendUtil.dart';
import '../../ble/protocol/iledcolor/source/graffiti/GraffitiSourceBean.dart';
import '../../ble/protocol/iledcolor/source/partition/PartitionSourceBean.dart';
import '../base/SendAndSaveWidget.dart';

//图片发送界面
class EditImagePage extends StatefulWidget {
  final GraffitiSourceBean sourceBean;

  const EditImagePage({super.key, required this.sourceBean});

  @override
  State<EditImagePage> createState() => _EditImagePageState();
}

class _EditImagePageState extends State<EditImagePage> {
  List<String> mode = [];

  @override
  Widget build(BuildContext context) {
    if (mode.isEmpty) {
      mode.add(S.of(context).str_id_9);
      mode.add(S.of(context).str_id_10);
      mode.add(S.of(context).str_id_11);
      mode.add(S.of(context).str_id_12);
      mode.add(S.of(context).str_id_13);
      mode.add(S.of(context).str_id_14);
      mode.add(S.of(context).str_id_15);
      mode.add(S.of(context).str_id_16);
    }
    return BaseWidget(
      child: Column(
        children: [
          TitleBarWidget(title: S.of(context).str_id_1),

          Expanded(
            child: Container(
              margin: EdgeInsets.all(10),
              child: SingleChildScrollView(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    //素材展示
                    SizedBox(
                      height: 150,
                      child: Image(
                        image: widget.sourceBean.memoryImage,
                        fit: BoxFit.contain,
                        filterQuality: FilterQuality.none,
                      ),
                    ),
                    //速度调节
                    Text(S.of(context).str_id_6, style: TextStyle(fontSize: 16)),
                    Slider(
                      max: 20,
                      min: 1,
                      value: 21 - widget.sourceBean.speed.toDouble(),
                      onChanged: (value) {
                        setState(() => widget.sourceBean.speed = 20 - value.toInt());
                      },
                    ),

                    //停留时间
                    Text(S.of(context).str_id_7, style: TextStyle(fontSize: 16)),
                    Slider(
                      value: widget.sourceBean.dwellTime.toDouble(),
                      min: 0.0,
                      max: 255,
                      onChanged: (double newValue) {
                        setState(() => widget.sourceBean.dwellTime = newValue.toInt());
                      },
                    ),

                    //特效
                    Row(
                      children: [
                        Text(S.of(context).str_id_8, style: TextStyle(fontSize: 16)),
                        Expanded(child: const SizedBox(width: 10)),
                        Expanded(
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              GestureDetector(
                                behavior: HitTestBehavior.translucent,
                                onTap: () {
                                  widget.sourceBean.effects--;
                                  if (widget.sourceBean.effects < 0) {
                                    widget.sourceBean.effects = mode.length - 1;
                                  }
                                  setState(() {});
                                },
                                child: Padding(
                                  padding: const EdgeInsets.all(8.0),
                                  child: Icon(Icons.arrow_back_ios_outlined),
                                ),
                              ),
                              Text(mode[widget.sourceBean.effects], style: TextStyle(fontSize: 16)),
                              GestureDetector(
                                behavior: HitTestBehavior.translucent,
                                onTap: () {
                                  widget.sourceBean.effects++;
                                  if (widget.sourceBean.effects >= mode.length) {
                                    widget.sourceBean.effects = 0;
                                  }
                                  setState(() {});
                                },
                                child: Padding(
                                  padding: const EdgeInsets.all(8.0),
                                  child: Icon(Icons.arrow_forward_ios_outlined),
                                ),
                              ),
                            ],
                          ),
                        ),
                      ],
                    ),
                    const SizedBox(height: 20),
                    //发送和保存
                    SendAndSaveWidget(
                      sendText: S.of(context).str_id_4,
                      saveText: S.of(context).str_id_5,
                      sendTap: () => SourceSendUtil.instance.sendSource(widget.sourceBean.castPartitionSourceBean()),
                      saveTap: () async {
                        //保存
                        var image = widget.sourceBean.getImage();

                        widget.sourceBean.setImagePath(
                          await FileUtil.save2File(
                            img.encodePng(image!),
                            "${widget.sourceBean.id}_${widget.sourceBean.sourceType}_0.png",
                          ),
                        );

                        SourceManager.instance.saveSource(widget.sourceBean);
                        Navigator.of(context).pop();
                        ToastUtil.showToast(S.of(context).str_id_17);
                      },
                    ),
                  ],
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }
}
